import env from '@/utils/env.js'
export default {
  // 为当前模块开启命名空间
  namespaced: true,
  
  state: () => ({
		// 客服电话--全局
		customerServiceNumber: env.customerServiceNumber,
		
		agreePrivacy: false, // 同意隐私协议
		
		// 登录相关
		userId: uni.getStorageSync('userId') || '',  // 用户id 从本地缓存中取出
		token: uni.getStorageSync('token') || '',  // token令牌
		openid: uni.getStorageSync('openid') || '',  // openid
		phone: uni.getStorageSync('phone') || '',  // phone
		// unionid: uni.getStorageSync('unionid') || '',  // unionid
		nickName: uni.getStorageSync('nickName') || '',  // 用户昵称
		avatar: uni.getStorageSync('avatar') || '',  // 用户头像
		isAuthent: uni.getStorageSync('isAuthent') || null,  // 用户是否实名
		openStatus: uni.getStorageSync('openStatus') || 0,  // 开关是否打开
		userType: uni.getStorageSync('userType') || 0,  // 用户类型 0普通用户 1商家
		loginBackPage: '',  // 登录后返回的页面
		
		backFocus: null,  // 记录用户关注操作，用于列表内更新（id和关注状态focusStatus
		backPraise: null,  // 记录动态点赞操作，用于列表内更新（id和点赞状态praiseStatus
		
		// 学校
		collegeId: uni.getStorageSync('collegeId') || null, // 学校id
		collegeName: uni.getStorageSync('collegeName') || '全部', // 学校name
		collegeSelectBackPage: '',  // 选择学校后返回的页面
		
		messageCount: 0, // 消息角标
  }),
  
  mutations: {
		
		// 更新同意隐私协议的状态，并存储到本地--未使用
		updateAgreePrivacy(state, value) {
			state.agreePrivacy = value
		},
		
		// 更新用户登录信息，并存储到本地
		updateUserLoginInfo(state, userLoginInfo) {
			state.userId = userLoginInfo.userId
			state.token = userLoginInfo.token
			state.nickName = userLoginInfo.nickName
			state.avatar = userLoginInfo.avatar
			state.isAuthent = userLoginInfo.isAuthent
			state.phone = userLoginInfo.phone
			state.userType = userLoginInfo.userType
			// state.openid = userLoginInfo.openid
			// state.unionid = userLoginInfo.unionid
			uni.setStorageSync('userId', state.userId)
			uni.setStorageSync('token', state.token)
			uni.setStorageSync('nickName', state.nickName)
			uni.setStorageSync('avatar', state.avatar)
			uni.setStorageSync('isAuthent', state.isAuthent)
			uni.setStorageSync('phone', state.phone)
			uni.setStorageSync('userType', state.userType)
			// uni.setStorageSync('openid', state.openid)
			// uni.setStorageSync('unionid', state.unionid)
		},
		// 清空用户登录信息, 并移除本地缓存
		removeUserLoginInfo(state) {
			state.userId = ''
			state.token = ''
			// state.openid = ''
			// state.unionid = ''
			// state.phone = ''
			uni.removeStorageSync('userId')
			uni.removeStorageSync('openid')
			// uni.removeStorageSync('token')
			// uni.removeStorageSync('unionid')
			// uni.removeStorageSync('phone')
		},
		// 更新用户昵称
		updateNickName(state, nickName) {
			state.nickName = nickName
			uni.setStorageSync('nickName', state.nickName)
		},
		// 更新用户头像
		updateAvatar(state, avatar) {
			state.avatar = avatar
			uni.setStorageSync('avatar', state.avatar)
		},
		// 更新用户绑定手机号和实名状态
		updatePhone(state, phone) {
			state.phone = phone
			state.isAuthent = true
			uni.setStorageSync('phone', state.phone)
			uni.setStorageSync('phone', state.isAuthent)
		},
		// 更新用户类型-普通用户和商家
		updateUserType(state, userType) {
			state.userType = userType
			uni.setStorageSync('userType', state.userType)
		},
		// 更新开关状态
		updateOpenStatus(state, openStatus) {
			state.openStatus = openStatus
			uni.setStorageSync('openStatus', state.openStatus)
		},
		
		// 更新选择学校后返回的页面
		updateCollegeSelectBackPage(state, page) {
			state.collegeSelectBackPage = page
			console.log(page)
		},
		// 更新学校id和name, 并存储到本地
		updateCollege(state, collegeInfo) {
			state.collegeId = collegeInfo.collegeId
			state.collegeName = collegeInfo.collegeName
			uni.setStorageSync('collegeId', state.collegeId)
			uni.setStorageSync('collegeName', state.collegeName)
		},
		// 更新消息角标
		updateMessageCount(state, value) {
			state.messageCount = value
		},
		
		// 更新登录相关信息
		updateLogin(state, params) {
			uni.setStorageSync('cancelLogin', false)
			this.commit('m_user/updateUserLoginInfo', params)
			uni.navigateBack({
				delta: 1,
				success: () => {
					// uni.showToast({
					// 	icon: 'none',
					// 	title: '登陆成功！'
					// })
				}
			})
		},
		
		// 更新返回的用户关注id和关注状态
		updateBackFocus(state, params) {
			state.backFocus = params
		},
		// 更新返回的动态点赞id和点赞状态
		updateBackPraise(state, params) {
			state.backPraise = params
		},
	},
  
  getters: {}
}